package com.example.ele_guarantee_hh_pro.comclass;
import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.core.CCService;
import com.cloudcc.deveditor.core.ServiceResult;
import com.cloudcc.deveditor.core.UserInfo;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.List;
/*公海池分配客户后台类*/
public class DistributionManager {

    private UserInfo userInfo;
    private CCService cs;
    public DistributionManager(UserInfo userInfo){
        this.userInfo=userInfo;
        cs=new CCService(userInfo);
    }
    /*
     * @Description 分配管户经理
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param managerId 当前经理的id,ids 分配的客户的id数组;psmc 交易中心的id
     * @return: null
     **/
    public String  distribute(String param) {


        JSONObject jsonObject = JSONObject.fromObject(param);
        String managerId = jsonObject.getString("managerId");
        JSONArray psmc_arr = jsonObject.getJSONArray("psmc");
        String ids=jsonObject.getString("ids");
        String[] split = ids.split(",");
        String gjzt="待跟进";
        String dfp="待分配";
        String msg="";
        String is_shared="否";
        String ghzt="未生效";
        try{
            ImportAccount importAccount = new ImportAccount(userInfo);
            if (split.length>0){
                msg="分配成功";
                for (int i = 0; i <split.length ; i++) {
                    List<CCObject> account1 = cs.cquery("Account", "id='" + split[i] + "'");
                    if(account1.size()>0){
                        String acc_zt = account1.get(0).get("follow_status") == null ? "" : account1.get(0).get("follow_status") + "";
                        String acc_name = account1.get(0).get("name") == null ? "" : account1.get(0).get("name") + "";
                        if(!acc_zt.equals(dfp)){
                            return "客户名称:"+acc_name+"客户状态不是待分配请在公海池进行分配操作";
                        }
                    }

                    for(int j=0;j<psmc_arr.size();j++){
                        CCObject account = new CCObject("Account");
                        String id=split[i];
                        String code="";
                        String province1="";
                        String fk_city_name1="";
                        String fk_country_name1="";
                        String lxdz="";
                        String psmc=psmc_arr.getJSONObject(j).getString("id");
                        //更新投标频率的所有人
                        List<CCObject> ele_num = cs.cquery("ele_num", "fk_account='" + id + "' and fk_psmc='" + psmc + "'");
                        if(ele_num.size()>0){
                            ele_num.get(0).put("ownerid",managerId);
                            cs.update(ele_num.get(0));
                        }



                        List<CCObject> account_info = cs.cquery("Account", "id='" + id + "'");
                        if(account_info.size()>0){
                            code=account_info.get(0).get("invoiceTaxNo")==null?"":account_info.get(0).get("invoiceTaxNo")+"";
                            province1=account_info.get(0).get("province")==null?"":account_info.get(0).get("province")+"";
                            fk_city_name1=account_info.get(0).get("fk_city_name")==null?"":account_info.get(0).get("fk_city_name")+"";
                            fk_country_name1=account_info.get(0).get("fk_country_name")==null?"":account_info.get(0).get("fk_country_name")+"";
                            lxdz=account_info.get(0).get("registeredAddress")==null?"":account_info.get(0).get("registeredAddress")+"";
                        }
                        account.put("id",id);
                        account.put("ownerid",managerId);
                        account.put("follow_status",gjzt);
                        cs.update(account);
                        CCObject manage_group = new CCObject("manage_group");
                        manage_group.put("name", code);
                        manage_group.put("fk_ccuser", managerId);
                        manage_group.put("fk_account", id);
                        manage_group.put("fk_psmc", psmc);
                        manage_group.put("province1", province1);
                        manage_group.put("fk_city_name1", fk_city_name1);
                        manage_group.put("fk_country_name1", fk_country_name1);
                        manage_group.put("lxdz", lxdz);
                        manage_group.put("is_shared", is_shared);
                        manage_group.put("ghzt", ghzt);
                        String gen = "";
                        if(!"null".equals(lxdz)){
                            if(!"".equals(lxdz)){
                                try {
                                    gen = importAccount.gen(lxdz);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                String[] split2 = gen.split(",");
                                manage_group.put("dlwzco", split2[0]);
                                manage_group.put("dlwzla", split2[1]);
                            }

                        }
                        cs.insert(manage_group);
                    }

                }
            }
        }catch ( Exception e){
            msg="分配异常："+e.toString();
        }

        return msg;


    }
    /*
     * @Description:获取可以分配的员工
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null:
     * @return: null
     **/
    public String getManagers() throws Exception{
        //获取当前操作用户的userid
        String userId = userInfo.getUserId();
        //查询 机构负责人是当前操作用户的 项目经理
        String sql = "select * from ccuser where is_deleted='0'" +
                " and jgfzr='" + userId + "'";
        List<CCObject> ccuser = cs.cqlQuery("ccuser", sql);
        String findCcuserInfo = "select employeenum from ccuser where id ='" + userId + "' and is_deleted='0' ";
        List<CCObject> ccuserInfo = cs.cqlQuery("ccuser", findCcuserInfo);
        String employeenum ="";
        if (ccuserInfo.size() > 0) {
            employeenum  = ccuserInfo.get(0).get("employeenum") == null ? "" : ccuserInfo.get(0).get("employeenum") + "";
        }
        /*公海池内部编号*/
        String TYPE = "00000";
        if (employeenum.equals(TYPE)){
            String findCCuserInfoList = "select id,name from ccuser where  jgfzr is null and is_deleted='0' ";
            List<CCObject> infoList = cs.cqlQuery("", findCCuserInfoList);
            JSONArray rs = new JSONArray();
            for (int i = 0; i < infoList.size(); i++) {
                JSONObject jsonObject = new JSONObject();
                String value = ccuser.get(i).get("id") == null ? "" : ccuser.get(i).get("id") + "";
                String label = ccuser.get(i).get("name") == null ? "" : ccuser.get(i).get("name") + "";
                jsonObject.put("value", value);
                jsonObject.put("label", label);
                rs.add(jsonObject);
            }
            return String.valueOf(rs);
        }else {
            JSONArray rs = new JSONArray();
            if (ccuser.size() > 0) {
                for (int i = 0; i < ccuser.size(); i++) {
                    JSONObject jsonObject = new JSONObject();
                    String value = ccuser.get(i).get("id") == null ? "" : ccuser.get(i).get("id") + "";
                    String label = ccuser.get(i).get("name") == null ? "" : ccuser.get(i).get("name") + "";
                    jsonObject.put("value", value);
                    jsonObject.put("label", label);
                    rs.add(jsonObject);
                }
            }
            return rs.toString();
        }
    }

    /*
     * @Description:获取公共交易中心的资源，通过选中的id，返回交易中心的name和id
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null:
     * @return: null
     **/
    public String getPsmc(String userid) throws Exception{
        //查询 机构负责人是当前操作用户的 项目经理
        List<CCObject> ghjl = cs.cquery("ghjl", "yh='"+userid+"'");
        JSONArray rs = new JSONArray();
        if(ghjl.size()>0){
            for (int i=0;i<ghjl.size();i++){
                JSONObject jsonObject = new JSONObject();
                String value = ghjl.get(i).get("jyzx") == null ? "" : ghjl.get(i).get("jyzx") + "";
                String label = ghjl.get(i).get("jyzxccname") == null ? "" : ghjl.get(i).get("jyzxccname") + "";
                jsonObject.put("value",value);
                jsonObject.put("label",label);
                rs.add(jsonObject);
            }
        }
        return rs.toString();
    }
}
